package com.greateamd.timetableasy.server.services;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.greateamd.timetableasy.client.services.university.UniversityService;
import com.greateamd.timetableasy.domain.Event;
import com.greateamd.timetableasy.domain.University;
import com.greateamd.timetableasy.domain.exceptions.DBServerSideException;
import com.greateamd.timetableasy.domain.exceptions.IllegalEntityStateException;
import com.greateamd.timetableasy.server.dao.UniversityDAO;

@Service("universityService")
public class UniversityServiceImpl implements UniversityService {
	
	private UniversityDAO universityDAO;
	public void setUniversityDAO(UniversityDAO universityDAO) {
		this.universityDAO = universityDAO;
	}
	
	@Override
	public boolean save(University university) throws DBServerSideException {
		try {
			universityDAO.save(university);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
		return true;
	}

	@Override
	public University find(int id) throws DBServerSideException{
		try {
			return universityDAO.getById(id);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public University findByName(String name) throws DBServerSideException {
		try {
			return universityDAO.findByName(name);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}


	@Override
	public List<University> getAll() throws DBServerSideException {
		try {
			return universityDAO.getAll();
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean remove(University university) throws DBServerSideException {
		try {
			universityDAO.delete(university);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public boolean update(University university) throws DBServerSideException {
		try {
			universityDAO.update(university);
			return true;
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}

	@Override
	public List<Event> getPlanning(University university, Date from, Date to)throws DBServerSideException, IllegalEntityStateException {
		try {
			return universityDAO.getPlanning(university, from, to);
		} catch (Exception e) {
			throw new DBServerSideException(e.getMessage());
		}
	}
}
